route.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ImportProcessor } from '@/app/lib/excel-import/import-processor';
  3. export async function POST(
  4. request: NextRequest,
  5. { params }: { params: Promise<{ id: string }> }
  6. ) {
  7. try {
  8. // Await the params promise to get the actual params
  9. const resolvedParams = await params;
  10. const importId = parseInt(resolvedParams.id);
  11. if (isNaN(importId)) {
  12. return NextResponse.json(
  13. { success: false, error: 'Invalid import ID' },
  14. { status: 400 }
  15. );
  16. }
  17. // Initialize import processor
  18. const processor = new ImportProcessor();
  19. // Validate import before processing
  20. const validation = await processor.validateImport(importId);
  21. if (!validation.valid) {
  22. return NextResponse.json(
  23. { success: false, error: validation.errors.join(', ') },
  24. { status: 400 }
  25. );
  26. }
  27. // Start processing in background
  28. processor.processImport(importId).catch(error => {
  29. console.error('Import processing failed:', error);
  30. });
  31. return NextResponse.json({
  32. success: true,
  33. message: 'Import process started successfully',
  34. importId
  35. });
  36. } catch (error) {
  37. console.error('Error triggering import:', error);
  38. return NextResponse.json(
  39. { success: false, error: 'Failed to trigger import' },
  40. { status: 500 }
  41. );
  42. }
  43. }